package cn.chinaunicom.sdsi.gacx.sys.service.serviceImpl;

import cn.chinaunicom.sdsi.gacx.sys.entity.GaSendMessEntity;
import cn.chinaunicom.sdsi.gacx.sys.service.GaSendMessService;
import cn.chinaunicom.sdsi.gacx.sys.dao.mybatis.GaSendMessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.chinaunicom.sdsi.frm.base.BaseService;
import com.github.pagehelper.PageInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class GaSendMessServiceImpl extends BaseService implements GaSendMessService{

	@Autowired
	private GaSendMessMapper gaSendMessMapper;
	/**
	 * 分页查询
	 */
	@Override
	public JSONObject search4PageList(GaSendMessEntity gaSendMessEntity,Integer start, Integer length){
		setPageInfo(start,length);
		List<GaSendMessEntity> beanList=gaSendMessMapper.search4PageList(gaSendMessEntity);
		PageInfo<GaSendMessEntity> page = new PageInfo<GaSendMessEntity>(beanList);
		JSONObject obj = null;
		JSONArray array = new JSONArray();
		
		for(GaSendMessEntity bl :beanList){
			obj = new JSONObject();
					obj.put("id",bl.getID());
					obj.put("toNum",bl.getTO_NUM());
					obj.put("toDate",bl.getTO_DATE());
					obj.put("sendNum",bl.getSEND_NUM());
			array.add(obj);
		}
		obj = new JSONObject();
		obj.element("data", array);
		obj.element("recordsTotal", page.getTotal());
		obj.element("recordsFiltered", page.getTotal());
		obj.element("draw", 0);
		return obj;
	}
	
	/**
	 * 查询单条数据
	 */
	@Override
	public GaSendMessEntity selectByPrimaryKey(GaSendMessEntity gaSendMessEntity){
		
		GaSendMessEntity bean=gaSendMessMapper.selectByPrimaryKey(gaSendMessEntity);
		return bean;
	}
	
	/**
	 * 删除单条数据
	 */
	@Override
	public int deleteByPrimaryKey(GaSendMessEntity gaSendMessEntity){
		int delnum = gaSendMessMapper.deleteByPrimaryKey(gaSendMessEntity);
		return delnum;
	}

	/**
	 * 保存数据
	 */
	@Override
	public int addObject(GaSendMessEntity gaSendMessEntity){
		int insertnum = gaSendMessMapper.addObject(gaSendMessEntity);
		return insertnum;
	}

	/**
	 * 修改单条数据
	 */
	@Override
	public int updateByPrimaryKey(GaSendMessEntity gaSendMessEntity){
		int updatetnum = gaSendMessMapper.updateByPrimaryKey(gaSendMessEntity);
		return updatetnum;
	}

	   /**
     * 保存数据
     */
    @Override
    public void addMessages(String content,List<String> to_nums,String send_num,String remark,String status){
        List <GaSendMessEntity> gaSendMessEntityList = new ArrayList<GaSendMessEntity>();
        if (to_nums != null && !to_nums.isEmpty()){
            for(String to_num: to_nums){
                GaSendMessEntity gaSendMessEntity = new GaSendMessEntity();
                gaSendMessEntity.setTO_NUM(to_num);
                gaSendMessEntity.setMESS(content);
                gaSendMessEntity.setSEND_NUM(send_num);
                gaSendMessEntity.setSTATUS(status);
                gaSendMessEntity.setREMARK(remark);
                gaSendMessEntity.setTO_DATE(new Date());
                gaSendMessEntityList.add(gaSendMessEntity);
            }
            if (gaSendMessEntityList !=null && !gaSendMessEntityList.isEmpty()){
                gaSendMessMapper.addMessages(gaSendMessEntityList);
            }
        }
    }
}